package com.cloudwise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudwise.entity.Menu;
import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.RoleMenu;
import com.cloudwise.service.MenuService;
import com.cloudwise.service.RoleMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * (RoleMenu)表控制层
 *
 * @author makejava
 * @since 2021-12-07 20:18:54
 */
@RestController
@RequestMapping("roleMenu")
public class RoleMenuController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private MenuService menuService;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param roleMenu 查询实体
     * @return 所有数据
     */
    @GetMapping
    public ReturnBean selectAll(Page<RoleMenu> page, RoleMenu roleMenu) {
        return success(this.roleMenuService.page(page, new QueryWrapper<>(roleMenu)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ReturnBean selectOne(@PathVariable Serializable id) {
        return success(this.roleMenuService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param roleMenu 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ReturnBean insert(@RequestBody RoleMenu roleMenu) {
        return success(this.roleMenuService.save(roleMenu));
    }

    /**
     * 修改数据
     *
     * @param roleMenu 实体对象
     * @return 修改结果
     */
    @PutMapping
    public ReturnBean update(@RequestBody RoleMenu roleMenu) {
        return success(this.roleMenuService.updateById(roleMenu));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ReturnBean delete(@RequestParam("idList") List<Long> idList) {
        return success(this.roleMenuService.removeByIds(idList));
    }
    @RequestMapping("findSelected")
    public ReturnBean findSelected(Integer roleId){
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<RoleMenu> list = roleMenuService.list(queryWrapper);
        List<Integer> menuIds = new ArrayList<>();
        for (RoleMenu roleMenu : list) {
            menuIds.add(roleMenu.getMenuId());
        }
        List<Integer> menuIdList = new LinkedList<>();
        for(Integer menuId : menuIds){
            QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<Menu>();
            menuQueryWrapper.eq("parent_id", menuId);
            List<Menu> menuList = menuService.list(menuQueryWrapper);
            if (menuList == null || menuList.size() == 0){
                menuIdList.add(menuId);
            }
        }
        return super.success(menuIdList);
    }
}

